package com.yys.apm.common.transfer;

import com.yys.apm.common.exception.BusinessException;

import java.lang.reflect.Field;
import java.lang.reflect.Proxy;

/**
 * @author Yingsheng Ye
 * @date 2021-03-24 18:05
 */
public class FactoryMapper {

    public static Object getMapper(Class clazz) {
        return Proxy.newProxyInstance(FactoryMapper.class.getClassLoader(), new Class[]{clazz},
                (proxy, method, args) -> {
                    if (args.length != 1) {
                        throw new BusinessException("参数异常");
                    }
                    Object sourceObj = args[0];
                    Class sourceClazz = sourceObj.getClass();
                    Field[] fields = sourceClazz.getDeclaredFields();

                    for (Field field : fields) {
                        field.setAccessible(true);//设置权限
                        Object value = field.get(sourceObj);
                        System.out.println(value.toString());
                    }

                    //获取方法注解
                    Mappings mappings = method.getAnnotation(Mappings.class);
                    Mapping[] mapping = mappings.value();
                    for (Mapping mapping1 : mapping) {
                        System.out.println(mapping1.source() + "-" + mapping1.target());
                    }
                    //获取方法
                    Class returnClazz = method.getReturnType();
                    Object targetObj = returnClazz.newInstance();
                    Field[] targetFields = returnClazz.getDeclaredFields();
                    for (Field targetField : fields) {
                        //获取set方法
                    }
                    return targetObj;
                });
    }

}
